package com.demo.dao.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.demo.model.Project;

public class ProjectMapper {

	public String insert(Project project) {
		return "insert into project (name, description,createTime) values (#{name},#{description},#{createTime})";
	}

	public String delete(long id) {
		BEGIN();
		DELETE_FROM("project");
		WHERE("id=#{id}");
		return SQL();
	}

	public String update(Project project) {
		BEGIN();
		UPDATE("project");
		if (project.getName() != null && project.getName().length() > 0) {
			SET("name=#{name}");
		}
		if (project.getDescription() != null && project.getDescription().length() > 0) {
			SET("description=#{description}");
		}
		if (project.getModifyTime() != null && project.getModifyTime().length() > 0) {
			SET("modifyTime=#{modifyTime}");
		}
		WHERE("id=#{id}");
		String sql = SQL();
		System.out.println(sql);
		return sql;
	}

	public String select(String name) {
		if (name == null || name.length() == 0) {
			return "select * from project order by id desc";
		} else {
			return "select * from project where name=#{name} order by id desc";
		}
	}

	public String selectOne(Integer id) {
		return "select * from project where id=#{id}";
	}
}
